package org.study.util;

import java.util.Collection;
import java.util.Map;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-06-10
 */
public final class Assert {
    private Assert() {
    }

    public static void isTrue(boolean expression, String errorMsg) throws IllegalArgumentException {
        if (!expression) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static void isNull(Object object, String errorMsg) throws IllegalArgumentException {
        if (object != null) {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static <T> T notNull(T object, String errorMsg) throws NullPointerException {
        if (object == null) {
            throw new NullPointerException(errorMsg);
        } else {
            return object;
        }
    }

    public static String notEmpty(String text, String errorMsg) throws IllegalArgumentException {
        if (text != null && text.length() != 0) {
            return text;
        } else {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static Object[] notEmpty(Object[] array, String errorMsg) throws IllegalArgumentException {
        if (array != null && array.length != 0) {
            return array;
        } else {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static <T> Collection<T> notEmpty(Collection<T> collection, String errorMsg) throws IllegalArgumentException {
        if (collection != null && !collection.isEmpty()) {
            return collection;
        } else {
            throw new IllegalArgumentException(errorMsg);
        }
    }

    public static <K, V> Map<K, V> notEmpty(Map<K, V> map, String errorMsg) throws IllegalArgumentException {
        if (map != null && !map.isEmpty()) {
            return map;
        } else {
            throw new IllegalArgumentException(errorMsg);
        }
    }
}
